package com.smbms.controller;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 处理全局异常
 */
@ControllerAdvice
public class ExceptionController {

    @ResponseBody//返回json数据
    @ExceptionHandler(Exception.class)//抓取异常
    public Object exceptionHandler(Exception e){
        //打印异常
        e.printStackTrace();

        //响应数据
        Map<String,Object> result=new HashMap<>();
        result.put("flag",false);
        result.put("msg","请求异常:"+e.getMessage());

        return result;
    }
}
